//
//  JZRegistViewController.m
//  QuanZiChat
//
//  Created by 张洪林 on 15/11/17.
//  Copyright © 2015年 ZhongHongLin. All rights reserved.
//

#import "JZRegistViewController.h"
#import "JZLoginViewController.h"
#import "LCPGetRongCloudToken.h"
@interface JZRegistViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userName;//用户名
@property (weak, nonatomic) IBOutlet UITextField *passWord;//密码
@property (weak, nonatomic) IBOutlet UITextField *passWord2;//确认密码
@property (weak, nonatomic) IBOutlet UITextField *email;//邮箱
@property (weak, nonatomic) IBOutlet UITextField *phoneNumber;//手机号

@end

@implementation JZRegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.userName resignFirstResponder];
    [self.passWord resignFirstResponder];
    [self.passWord2 resignFirstResponder];
    [self.email resignFirstResponder];
    [self.phoneNumber resignFirstResponder];
}


//确认注册
- (IBAction)confirmTheRegistion:(id)sender {
    
    AVUser *user = [AVUser user];
    user.username = self.userName.text;
    user.password = self.passWord.text;
    user.email = self.email.text;
    user.mobilePhoneNumber = self.phoneNumber.text;
    

    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            
            //生成Token
            LCPGetRongCloudToken *token = [[LCPGetRongCloudToken alloc] init];
            token.app_key = @"pwe86ga5ed636";
            token.appSecret = @"YvVKTf43E05v";
            [token getTokenWithUserId:[AVUser currentUser].objectId Name:self.userName.text     PortraitUri:@"" CallBack:^(id token) {
                NSLog(@"%@",token);
                AVUser *user = [AVUser currentUser];
                [user setObject:token forKey:@"Token"];
                [user saveInBackground];
            }];

            
            NSLog(@"注册成功");
            
            UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"恭喜" message:@"注册成功" preferredStyle:(UIAlertControllerStyleAlert)];
            
            UIAlertAction * alertAction1 = [UIAlertAction actionWithTitle:@"返回登录界面" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                
                //跳转到登录界面
                //JZLoginViewController * loginVC = [self.storyboard instantiateViewControllerWithIdentifier:@"JZLoginViewController"];
                
                [self dismissViewControllerAnimated:YES completion:nil];
            }];
            
            
            [alertController addAction:alertAction1];
            
            
            [self presentViewController:alertController animated:YES completion:nil];
            
        } else {
            
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"注册失败" message:[NSString stringWithFormat:@"%@", error] preferredStyle:(UIAlertControllerStyleAlert)];
            
            UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            
            [alertController addAction:alertAction];
            
            [self presentViewController:alertController animated:YES completion:nil];
            
            NSLog(@"注册失败%@", error);
        }
    }];
}


//取消注册
- (IBAction)cancleRegist:(id)sender {
    
    [self dismissViewControllerAnimated:NO completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
